import axios from "axios";
import Vue from "vue"
import Syx from "@/utils/syxui/syx-ui.umd.min.js";
import router from '@/router'
let { Message } = Syx.ElementUI;

axios.defaults.headers["Content-Type"] = "application/json;charset=utf-8";

const service = axios.create({
  baseURL: process.env.VUE_APP_DOMAIN_URL,
  timeout: 30000,
});

service.interceptors.request.use(
  (config) => {
    // 是否需要设置 token
      let token = Vue.prototype.nativeplacestore.get("login");
      let t = JSON.parse(token)
      if (t.code==1000) {
        config.headers['Authorization'] = "Bearer " + t['data']
      }
    return config;
  },
  (error) => {
    Promise.reject(error);
  }
);

// 响应拦截器
service.interceptors.response.use(
  (res) => {
    if (res.data.code == 1000) {
      return res.data.data;
    } else {
      Message({
        showClose: true,
        message: res.msg,
        type: "warning",
      });
      return res.data;
    }
  },
  (error) => {
      Message({
          showClose: true,
          message: "请重新登录",
          type: "warning",
      });
      router.push('/login')
      return Promise.reject(error)
  }
);
export default service;
